import cors from 'koa2-cors';
import compress from 'koa-compress';
import bodyParser from 'koa-bodyparser';
import resourcesProxy from './proxy/resourcesProxy';
import portProxy from './proxy/portProxy';
import errHandler from './lib/errHandler';
import appFileRoutes from '../routes/AppFileRoutes';
import cacheRoutes from '../routes/CacheRoutes';
import DBRoutes from '../routes/DBRoutes';
import EncryptRoutes from '../routes/EncryptRoutes';
import configTool from '../com/configTool';


function clientMiddleWare(app: any) {
    app.use(errHandler());

    // 允许跨域请求
    configTool.config.clientCORS && app.use(cors());

    app.use(compress({br: false}));

    resourcesProxy(app);
    portProxy(app);

    // 最后再进行 post 请求格式化
    app.use(bodyParser());

    // 数据转发缓存服务
    configTool.config.CacheSave.state && app.use(cacheRoutes.routes());


    app.use(appFileRoutes.routes());
    app.use(DBRoutes.routes());
    app.use(EncryptRoutes.routes());
}

export default clientMiddleWare;
